#!/bin/bash

# 卸载腾讯云监控服务

./usr/local/qcloud/stargate/admin/uninstall.sh
./usr/local/qcloud/YunJing/uninst.sh
./usr/local/qcloud/monitor/barad/admin/uninstall.sh



# Prometheus

# Yum docker
# docker pull prom/prometheus-linux-amd64
# docker run -itd --name myprometheus --network allinpay --restart=always -p 9090:9090 prom/prometheus-linux-amd64

# Yum service
# 步骤1：创建一个Prometheus用户和组
useradd -m -s /bin/false prometheus

# 步骤2：Prometheus创建配置目录
mkdir /etc/prometheus
mkdir /var/lib/prometheus
chown prometheus /var/lib/prometheus/

# 步骤3：下载Prometheus tar文件
wget https://github.com/prometheus/prometheus/releases/download/v2.43.0-rc.1/prometheus-2.43.0-rc.1.linux-amd64.tar.gz

tar zxvf prometheus-2.43.0-rc.1.linux-amd64.tar.gz
cd  prometheus-2.43.0-rc.1.linux-amd64
cp prometheus  /usr/local/bin
cp promtool  /usr/local/bin

# 步骤4：Prometheus创建配置文件
cat > /etc/prometheus/prometheus.yml <<EOF
# Global config
global:
  scrape_interval: 15s        #设置间隔为每15秒。默认值为每1分钟。
  Evaluation_interval: 15s    #每15秒评估一次规则。默认值为每1分钟。
  scrape_timeout: 15s         #scrape_timeout设置为全局默认值(10s)。

scrape_configs:
  - job_name: 'prometheus'
    static_configs:
    - targets: ['localhost:9090']
  - job_name: 'node_exporter'
    static_configs:
    - targets: ['localhost:9090']
EOF

firewall-cmd --add-port=9090/tcp --permanent
firewall-cmd --reload

# 步骤5：Prometheus Server创建Systemd服务文件
cat > /etc/systemd/system/prometheus.service <<EOF
[Unit]
Description=Prometheus Time Series Collection and Processing Server
Wants=network-online.target
After=network-online.target

[Service]
User=prometheus
Group=prometheus
Type=simple
ExecStart=/usr/local/bin/prometheus \
    --config.file /etc/prometheus/prometheus.yml \
    --storage.tsdb.path /var/lib/prometheus/ \
    --web.console.templates=/etc/prometheus/consoles \
    --web.console.libraries=/etc/prometheus/console_libraries

[Install]
WantedBy=multi-user.target
EOF

systemctl daemon-reload
systemctl start prometheus
systemctl enable prometheus
systemctl status  prometheus


# 步骤6：安装并配置node_exporter
useradd -m -s /bin/false node_exporter
wget https://github.com/prometheus/node_exporter/releases/download/v1.5.0/node_exporter-1.5.0.linux-amd64.tar.gz
tar zxvf node_exporter-1.5.0.linux-amd64.tar.gz
cp node_exporter-1.5.0.linux-amd64/node_exporter /usr/local/bin
chown node_exporter:node_exporter /usr/local/bin/node_exporter
cat > /etc/systemd/system/node_exporter.service <<EOF
[Unit]
Description=Prometheus Node Exporter
Wants=network-online.target
After=network-online.target

[Service]
User=node_exporter
Group=node_exporter
Type=simple
ExecStart=/usr/local/bin/node_exporter

[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload
systemctl start node_exporter
systemctl enable node_exporter
systemctl status node_exporter

firewall-cmd --add-port=9100/tcp  --permanent
firewall-cmd --reload